<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- 课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <!-- 一级类别 开始-->
              <ul class="clearfix">
                <li :class="{current:!$route.query.subjectParentId}">
                  <a
                    title="全部"
                    href="javascript:void(0);">全部</a>
                </li>
                <li v-for=" nestedTree in nestedTreeList" :key="nestedTree.id" :class="{current:$route.query.subjectParentId===nestedTree.id}"> >
                  <a
                    :title="nestedTree.title"
                    href="javascript:void(0);"
                    @click="chooseSubject(nestedTree.id)">{{ nestedTree.title }}</a>
                </li>
              </ul>
              <!-- /一级类别 结束-->
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"/>
            </dt>
            <dd class="c-s-dl-li">
              <!-- 二级类别 开始-->
              <ul class="clearfix">
                <li :class="{current:!$route.query.subjectId}">
                  <a
                    title="全部"
                    href="javascript:void(0);"
                    @click="chooseSecondSubject('')">全部</a>
                </li>
                <li v-for=" (secondSubject) in secondSubjectList" :class="{current:$route.query.subjectId===secondSubject.id}" :key="secondSubject.id">
                  <a
                    :title="secondSubject.title"
                    href="javascript:void(0);"
                    @click="chooseSecondSubject(secondSubject.id)">{{ secondSubject.title }}</a>
                </li>
              </ul>
              <!-- /二级类别 结束-->
            </dd>
          </dl>
          <div class="clear"/>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <!-- 排序 开始-->
            <ol class="js-tap clearfix">
              <li :class="{'current bg-green': $route.query.buySort}">
                <a title="销量" href="javascript:void(0);" @click="chooseSort('buySort',$route.query.buySort)">销量
                  <i v-if="$route.query.buySort==='0'">↓</i>
                  <i v-if="$route.query.buySort==='1'">↑</i>
                </a>
              </li>
              <li :class="{'current bg-green': $route.query.newSort}">
                <a title="最新" href="javascript:void(0);" @click="chooseSort('newSort', $route.query.newSort)">最新
                  <i v-if="$route.query.newSort==='0'">↓</i>
                  <i v-if="$route.query.newSort==='1'">↑</i>
                </a>
              </li>
              <li :class="{'current bg-green': $route.query.priceSort}">
                <a title="价格" href="javascript:void(0);" @click="chooseSort('priceSort', $route.query.priceSort)">价格
                  <i v-if="$route.query.priceSort==='0'">↓</i>
                  <i v-if="$route.query.priceSort==='1'">↑</i>
                </a>
              </li>
            </ol>
            <!-- /排序 结束-->
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section v-if="!items" class="no-data-wrap">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam">没有相关数据，小编正在努力整理中...</span>
          </section>
          <!-- /无数据提示 结束-->

          <!-- 数据列表 开始-->
          <article v-if="items" class="comm-course-list" >
            <ul id="bna" class="of">
              <li v-for=" course in items" :key="course.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img :alt="course.title" :src="course.cover" class="img-responsive">
                    <div class="cc-mask">
                      <a :href="'/course/'+course.id" title="开始学习" class="comm-btn c-btn-1">开始学习</a>
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a :href="'/course/'+course.id" :alt="course.title" class="course-title fsize18 c-333">{{ course.title }}</a>
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span class="fr jgTag bg-green">
                      <i class="c-fff fsize12 f-fA">{{ course.price==0?'免费':course.price }}</i>
                    </span>
                    <!-- <span class="fr jgTag ">
                      <i class="c-orange fsize12 f-fA"> ￥99</i>
                    </span> -->
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{ course.buyCount }}人学习</i>
                      |
                      <i class="c-999 f-fA">{{ course.buyCount }}人购买</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"/>
          </article>
          <!-- /数据列表 结束-->
        </div>
      </section>

    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>
import courseApi from '~/api/course'
import subjectApi from '~/api/subject'
import querystring from 'querystring' // url参数拼接工具
export default {
  // 服务器端渲染方案：
  // 异步数据获取：在前端服务器端执行
  data() {
    return {
      nestedTreeList: [],
      secondSubjectList: [],
      items: [],
      newIndex: ''
    }
  },
  async asyncData(page) {
    // 组装查询参数
    const searchObj = {}

    // 从url地址栏中获取查询参数
    const query = page.route.query
    searchObj.subjectParentId = query.subjectParentId || ''
    searchObj.subjectId = query.subjectId || ''
    searchObj.buySort = query.buySort || ''
    searchObj.newSort = query.newSort || ''
    searchObj.priceSort = query.priceSort || ''
    searchObj.courseName = query.courseName || ''
    // 获取课程列表
    const courseListResponse = await courseApi.getListBySearchObj(searchObj)
    console.log('1111' + query.buySort)
    let items = []
    items = courseListResponse.data.items
    // 获取课程分类列表
    let nestedTreeList = []
    let secondSubjectList = []
    const subjectListResponse = await subjectApi.getNestedTreeList()
    nestedTreeList = subjectListResponse.data.items
    if (searchObj.subjectParentId) {
      var list = nestedTreeList.find(tree => tree.id === searchObj.subjectParentId)
      secondSubjectList = list.children
    } else {
      secondSubjectList = subjectListResponse.data.secondSubjectList
    }
    return {
      nestedTreeList,
      secondSubjectList,
      items,
      searchObj
    }
  },
  methods: {
    chooseSubject(id) {
      console.log(id)

      window.location = 'course?subjectParentId=' + id
    },
    chooseSecondSubject(id) {
      this.searchObj.id = id
      // 自动组装queryString
      const obj = {
        subjectParentId: this.searchObj.subjectParentId,
        subjectId: id
      }
      const querys = querystring.stringify(obj)
      // console.log(querys)
      window.location = '/course?' + querys
    },
    chooseSort(type, sort) {
      console.log(type)
      console.log(sort)
      let obj = {}
      let querys = {}
      if (type === 'buySort') {
        // 自动组装queryString
        obj = {
          subjectParentId: this.searchObj.subjectParentId,
          subjectId: this.searchObj.subjectId,
          buySort: sort === '0' ? 1 : 0
        }
        querys = querystring.stringify(obj)
      } if (type === 'newSort') {
        // 自动组装queryString
        obj = {
          subjectParentId: this.searchObj.subjectParentId,
          subjectId: this.searchObj.subjectId,
          newSort: sort === '0' ? 1 : 0
        }
        querys = querystring.stringify(obj)
      } if (type === 'priceSort') {
        // 自动组装queryString
        obj = {
          subjectParentId: this.searchObj.subjectParentId,
          subjectId: this.searchObj.subjectId,
          priceSort: sort === '0' ? 1 : 0
        }
        querys = querystring.stringify(obj)
      }
      window.location = '/course?' + querys
    }
  }
}
</script>
